package com.test.rxjavademo;

import android.util.Log;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

public class TransOperatorDemo {

    public static final String TAG = "TransOperatorDemo";

    Observer<Object> observer = new Observer<Object>() {
        @Override
        public void onSubscribe(Disposable d) {
            //建立订阅后会调用该方法：相当于是订阅前置操作
            Log.e(TAG, "onSubscribe........");
        }

        @Override
        public void onNext(Object o) {
            Log.e(TAG, "onNext......" + o.toString());
        }

        @Override
        public void onError(Throwable e) {
            Log.e(TAG, "onError......." + e.toString());
        }

        @Override
        public void onComplete() {
            Log.e(TAG, "onComplete.......");
        }
    };

    public void test1(){
        Observable.just("1111","22222")
                //map：对被观察者进行处理，把原来发射出来的事件进行处理并产生新的事件，，再此创建新的被观察者，发射事件。
                .map(new Function<String, Object>() {
                    @Override
                    public Object apply(String s) throws Exception {
                        return (s + "已处理");
                    }
                }).subscribe(observer);
    }

    public void test2(){
        Observable.just("register")
                .flatMap(new Function<String, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(String s) throws Exception {
                        Log.e(TAG, "apply: " + "注册成功" );
                        return Observable.just("请求登录");
                    }
                }).subscribe(observer);
    }

    public void test3(){
        Observable.just("11111","2222","33333","4444","5555","6666")
                //flatMap 不保证事件的顺序   concatMap保证事件的顺序
                .concatMap(new Function<String, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(String s) throws Exception {
                        return Observable.just(s + "已处理");
                    }
                }).subscribe(observer);
    }
}
